package com.seeTools.boot.common.core;

import com.alibaba.fastjson.serializer.*;

import java.text.DecimalFormat;

/**
 * @author xiaotong
 */
public class CustomerBigDecimalCode extends BigDecimalCodec implements ContextObjectSerializer {

    public final static CustomerBigDecimalCode instance = new CustomerBigDecimalCode();

    /**
     * 当BigDecimal类型的属性上有@JsonFiled注解，且该注解中的format有值时，使用该方法进行序列化，否则使用fastjson的
     * BigDecimalCodec中的write方法进行序列化
     */
    @Override
    public void write(JSONSerializer serializer, Object object, BeanContext context) {
        SerializeWriter out = serializer.out;
        if (object == null) {
            out.writeString("");
            return;
        }
        String format = context.getFormat();
        DecimalFormat decimalFormat = new DecimalFormat(format);
        out.writeString(decimalFormat.format(object));
    }
}
